import { createRouter, createWebHashHistory } from "vue-router"

import VideoList from '~/pages/file/videoList.vue'
import ExamSign from '~/pages/sign/examSign.vue'
import TeachPlanSign from '~/pages/sign/teachPlanSign.vue'
import CourseSign from '~/pages/sign/courseSign.vue'
import Admin from "~/layouts/admin.vue";
import Index from "~/pages/index.vue"
import Login from "~/pages/login.vue"
import Pdf from "~/pages/pdf.vue"
import NotFound from "~/pages/404.vue"
import CourseList from '~/pages/course/courseList.vue'
import LiveList from '~/pages/course/liveList.vue'
import TypeList from '~/pages/course/typeList.vue'
import StuFeedbackList from '~/pages/course/stuFeedbackList.vue'
import ManagerList from '~/pages/user/user/managerList.vue'
import AdminList from '~/pages/user/user/adminList.vue'
import TeacherList from '~/pages/user/user/teacherList.vue'
import StudentList from '~/pages/user/user/studentList.vue'
import TeachEvaluateList from '~/pages/user/user/teachEvaluateList.vue'
import StuHonorList from '~/pages/user/user/stuHonorList.vue'
import RoleList from '~/pages/user/roleList.vue'
import Config from '~/pages/user/config.vue'
import ExamList from '~/pages/exam/examList.vue'
import ScoreList from '~/pages/score/scoreList.vue'
import TrueResult from "~/pages/score/trueResult.vue"
import QuestionList from '~/pages/question/questionList.vue'
import QuestionDetail from '~/pages/question/questionDetail.vue'
import ExamDetail from '~/pages/exam/questionDetail.vue'
import ClassList from '~/pages/class/classList.vue'
import ClassCourseList from '~/pages/class/classCourseList.vue'
import ClassLogList from '~/pages/class/classLogList.vue'
import FilesList from '~/pages/file/filesList.vue'
import LessonNoteList from '~/pages/file/lessonNoteList.vue'
import receiveAnnounceList from '~/pages/announce/receiveAnnounceList.vue'
import PrintList from '~/pages/print/printList.vue'
import Meeting from '~/pages/print/meeting.vue'
import sendAnnounceList from '~/pages/announce/sendAnnounceList.vue'
import LivePlayback from '~/pages/file/livePlayback.vue'
import monthPlan from "~/pages/print/monthPlan.vue";
import projectPlan from "~/pages/print/projectPlan.vue";

const routes = [
    {
        path: "/",
        name: "admin",
        component: Admin,
    },
    {
        path: "/login",
        component: Login,
        meta: {
            title: "登录页"
        }
    }, {
        path: "/pdf",
        component: Pdf,
        meta: {
            title: "pdf打印"
        }
    }, {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }
]

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [{
    path: "/",
    name: "/",
    component: Index,
    meta: {
        title: "首页"
    }
}, {
    path: "/user/user/adminList",
    name: "/user/user/adminList",
    component: AdminList,
    meta: {
        title: "管理员列表"
    }
}, {
    path: "/user/user/managerList",
    name: "/user/user/managerList",
    component: ManagerList,
    meta: {
        title: "培训负责人列表"
    }
}, {
    path: "/user/user/teacherList",
    name: "/user/user/teacherList",
    component: TeacherList,
    meta: {
        title: "培训讲师列表"
    }
}, {
    path: "/user/user/studentList",
    name: "/user/user/studentList",
    component: StudentList,
    meta: {
        title: "学员列表"
    }
}, {
    path: "/user/roleList",
    name: "/user/roleList",
    component: RoleList,
    meta: {
        title: "角色列表"
    }
}, {
    path: "/user/config",
    name: "/user/config",
    component: Config,
    meta: {
        title: "配置列表"
    }
}, {
    path: "/course/typeList",
    name: "/course/typeList",
    component: TypeList,
    meta: {
        title: "课程类型"
    }
}, {
    path: "/course/courseList",
    name: "/course/courseList",
    component: CourseList,
    meta: {
        title: "课程列表"
    }
}, {
    path: "/exam/examList",
    name: "/exam/examList",
    component: ExamList,
    meta: {
        title: "试卷列表"
    }
}, {
    path: "/score/scoreList",
    name: "/score/scoreList",
    component: ScoreList,
    meta: {
        title: "成绩列表"
    }
},{
    path: "/score/trueResult",
    name: "/score/trueResult",
    component: TrueResult,
    meta: {
        title: "答题结果",
    },
}, {
    path: "/question/questionList",
    name: "/question/questionList",
    component: QuestionList,
    meta: {
        title: "题目列表"
    }
}, {
    path: "/class/classList",
    name: "/class/classList",
    component: ClassList,
    meta: {
        title: "班级列表"
    }
},{
    path: "/class/classLogList",
    name: "/class/classLogList",
    component: ClassLogList,
    meta: {
        title: "办班总结"
    }
}, {
    path: "/file/filesList",
    name: "/file/filesList",
    component: FilesList,
    meta: {
        title: "培训资料"
    }
}, {
    path: "/file/lessonNoteList",
    name: "/file/lessonNoteList",
    component: LessonNoteList,
    meta: {
        title: "培训教案"
    }
}, {
    path: "/announce/sendAnnounceList",
    name: "/announce/sendAnnounceList",

    component: sendAnnounceList,
    meta: {
        title: "发送通知"
    }
}, {
    path: "/announce/receiveAnnounceList",
    name: "/announce/receiveAnnounceList",

    component: receiveAnnounceList,
    meta: {
        title: "接受通知"
    }
}, {
    path: "/print/printList",
    name: "/print/printList",
    component: PrintList,
    meta: {
        title: "档期管理"
    }
}, {
    path: "/file/videoList",
    name: "/file/videoList",
    component: VideoList,
    meta: {
        title: "课件视频"
    }
}, {
    path: "/class/classCourseList",
    name: "/class/classCourseList",
    component: ClassCourseList,
    meta: {
        title: "班级课程"
    }
}, {
    path: "/sign/courseSign",
    name: "/sign/courseSign",
    component: CourseSign,
    meta: {
        title: "课程签到"
    }
},
{
    path: "/sign/examSign",
    name: "/sign/examSign",
    component: ExamSign,
    meta: {
        title: "考试签到"
    }
},{
    path: "/sign/teachPlanSign",
    name: "/sign/teachPlanSign",
    component: TeachPlanSign,
    meta: {
        title: "资料签到"
    }
}, {
    path: "/course/liveList",
    name: "/course/liveList",
    component: LiveList,
    meta: {
        title: "在线微课"
    },
},
{
    path: "/file/livePlayback",
    name: "/file/livePlayback",
    component: LivePlayback,
    meta: {
        title: "录播学习"
    },
},
{
    path: "/exam/questionDetail",
    name: "/exam/questionDetail",
    component: ExamDetail,
    meta: {
        title: "试卷详情"
    }
}, {
    path: "/question/questionDetail",
    name: "/question/questionDetail",
    component: QuestionDetail,
    meta: {
        title: "题目详情"
    }
}, {
    path: "/user/user/teachEvaluateList",
    name: "/user/user/teachEvaluateList",
    component: TeachEvaluateList,
    meta: {
        title: "培训讲师评价",
    }
}, {
    path: "/user/user/stuHonorList",
    name: "/user/user/stuHonorList",
    component: StuHonorList,
    meta: {
        title: "荣誉证书"
    }
}, {
    path: "/course/stuFeedbackList",
    name: "/course/stuFeedbackList",
    component: StuFeedbackList,
    meta: {
        title: "学员反馈"
    }
}, {
    path: "/print/meeting",
    name: "/print/meeting",
    component: Meeting,
    meta: {
        title: "会议纪要"
    }
},{
    path: "/print/monthPlan",
    name: "/print/monthPlan",
    component: monthPlan,
    meta: {
        title: "月度计划"
    }
},{
    path: "/print/projectPlan",
    name: "/print/projectPlan",
    component: projectPlan,
    meta: {
        title: "项目计划"
    }
}]

export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 动态添加路由的方法
// 导出一个函数，用于添加路由
export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false
    // 根据菜单数组查找并添加路由
    const findAndAddRoutesByMenus = (arr) => {
        arr.forEach(e => {
            // 在异步路由中查找与菜单路径相同的路由
            let item = asyncRoutes.find(o => o.path == e.pagePath)
            // 如果找到了且路由中没有该路径，则添加该路由
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("admin", item)
                hasNewRoutes = true
            }
            // 如果菜单中有子菜单，则递归调用该函数
            if (e.menuDtos && e.menuDtos.length > 0) {
                findAndAddRoutesByMenus(e.menuDtos)
            }
        })
    }

    // 调用函数，传入菜单数组
    findAndAddRoutesByMenus(menus)

    // 手动添加asyncRoutes中ClassCourseList的路由
    asyncRoutes.forEach(item => {
        if (!router.hasRoute(item.path) && (item.path === "/class/classCourseList" || item.path === "/exam/questionDetail" || item.path === "/question/questionDetail" || item.path === "/class/classLogList" || item.path === "/user/user/teachEvaluateList" || item.path === "/course/stuFeedbackList" || item.path === "/user/user/stuHonorList" || item.path === "/score/trueResult")) {
            router.addRoute("admin", item);
            hasNewRoutes = true;
        }
    });

    // 返回是否有新的路由
    return hasNewRoutes
}
